<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-title">校园拼车系统</div>
      <el-form :model="loginForm" :rules="loginRules" ref="loginFormRef" class="login-form">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入手机号" prefix-icon="User" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" prefix-icon="Lock" show-password />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loading" class="login-button" @click="handleLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { login } from '@/api/user'

const router = useRouter()
const loginFormRef = ref(null)
const loading = ref(false)

// 登录表单数据
const loginForm = reactive({
  username: '',
  password: ''
})

// 表单验证规则
const loginRules = {
  username: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern:/^1[3-9]\d{9}$|^adminRoot$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}
// 登录方法
const handleLogin = async () => {
  try {
    // 表单验证
    await loginFormRef.value.validate()
    
    loading.value = true
    
    try {
      // 调用登录API
      const res = await login(loginForm)
      
      // 登录成功
      if (res.code === 1) {
        const userInfo = res.data.user
        const token = res.data.token
        
        // 保存token和用户信息到本地存储
        localStorage.setItem('token', token)
        localStorage.setItem('userInfo', JSON.stringify(userInfo))
        
        // 根据用户身份跳转到不同页面
        if (userInfo.isAdmin) {
          router.push('/monitor')
          ElMessage.success('管理员登录成功')
        } else if (userInfo.isDriver) {
          router.push('/account')
          ElMessage.success('司机登录成功')
        } else {
          // router.push('/booking')
          ElMessage.error('无登录权限')
        }
      } else {
        ElMessage.error(res.msg || '登录失败')
      }
    } catch (error) {
      console.error('登录失败', error)
      ElMessage.error(error.message || '登录失败，请稍后再试')
    } finally {
      loading.value = false
    }
  } catch (error) {
    console.error('登录验证失败', error)
  }
}
</script>

<style scoped>
html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  width: 100vw;
  margin: 0;
  padding: 0;
  background: #f0f2f5;
  background-image: url('https://img.freepik.com/free-vector/city-skyline-concept-illustration_114360-8923.jpg?w=2000');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.login-box {
  width: 100%;
  max-width: 400px;
  padding: 40px;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-title {
  font-size: 28px;
  font-weight: bold;
  text-align: center;
  margin-bottom: 30px;
  color: var(--primary-color);
}

.login-form {
  margin-top: 20px;
  width: 100%;
}

.login-button {
  width: 100%;
}
</style> 